def avg(list):
    return (float)(sum(list))/(float)(len(list))

def sum(list):
    sum = 0;
    for i in list:
        sum+= i;
    return sum

def var(list):
   mean = avg(list)
   squared_diffs = []
   for i in list:
       squared_diffs.append((i-mean)**2)
   return ((float)(sum(squared_diffs))/(float)(len(list)-1))

def avg2(list, index):
    "return the average of a particular column from an array of arrays"
    newlist = []
    for i in list:
        newlist.append(i[index])
    return avg(newlist)

def matrix_avg(matrix):
    vals = []
    averages = []
    
    #populate vals with empty frames
    for i in range(len(matrix[0])):
        vals.append([])
    
    #populate vals with matrix data
    for i,row in enumerate(matrix):
        for j,col in enumerate(row):
            vals[j].append(col)
            
    #compute averages of each column
    for i,col in enumerate(vals):
        averages.append(avg(col))
    
    return averages

def matrix_var(matrix):
    vals = []
    variances = []
    
    #populate vals with empty frames
    for i in range(len(matrix[0])):
        vals.append([])
    
    #populate vals with matrix data
    for i,row in enumerate(matrix):
        for j,col in enumerate(row):
            vals[j].append(col)
            
    #compute averages of each column
    for i,col in enumerate(vals):
        variances.append(var(col))
    
    return variances

def pretty_print(matrix):
    for row in matrix:
        s = ""
        for val in row:
            s += str('%15.3f'%(val)) + ", "
        print s